//
//  AppListViewController.m
//  LimitFree
//
//  Created by Hailong.wang on 15/8/26.
//  Copyright (c) 2015年 Hailong.wang. All rights reserved.
//

#import "AppListViewController.h"
#import "JHRefresh.h"
#import "ApplicationTableViewCell.h"
#import "SettingViewController.h"
#import "AppInfoViewController.h"

@interface AppListViewController ()<UITableViewDataSource,UITableViewDelegate> {
    
    UITableView* _tableView;
    
   // NSMutableArray* _data;
    
 
     NSString* _categoryID;
    //是加载更多
   // BOOL _isloadingMore;
    //是刷新
    
    
    //BOOL _isRefresh;
    
    
}
//是加载更多
@property (nonatomic, assign) BOOL isLoadingMore;
//是刷新
@property (nonatomic, assign) BOOL isRefresh;
@end

@implementation AppListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)createView {
   
    
    UIButton *leftBtn = [Factory createButtonWithTitle:@"分类" frame:CGRectMake(0, 5, 40, 30) titleFont:12.f textColor:[UIColor blackColor] backgroundColor:RGB(200, 200, 200) target:self selector:@selector(toShowAppClassify)];
    [self createNavigationLeftButton:leftBtn];
    
    UIButton *rightBtn = [Factory createButtonWithTitle:@"设置" frame:CGRectMake(0, 5, 40, 30) titleFont:12.f textColor:[UIColor blackColor] backgroundColor:RGB(200, 200, 200) target:self selector:@selector(toAppShowSetting)];
    [self createNavigationRightButton:rightBtn];
    [self createTableView];
    
    //添加刷新控件
    [self createRefreshView];
}
//添加刷新控件
-(void)createRefreshView{
    //避免强引用 用弱引用 //否则内存不会被泄露
    __weak typeof (self) weakSelf=self;
    
    //头部
    [_tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.isRefresh=YES;
        NSLog(@"下拉");
        //刷新 获取当前的分类的最新信息
        [weakSelf resetParame];
        //调用请求
        [weakSelf getNetData];
    }];
    //底部
    [_tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.isLoadingMore=YES;
        //增加页码
        //NSLog(@"上拉");
        weakSelf.page++;
        //获取网络请求
        [weakSelf getNetData];
    }];
 
}
//获取网络数据
-(void)getNetData{
    //用来让子视图重写，在俯视图没有任何作用
}

//重置请求参数
-(void)resetParame{
   //用来让子视图重写
}
-(void)createTableView{
    _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    [self.view addSubview:_tableView];
}
- (void)toShowAppClassify {
   //不做实现，值为消除警告
}

- (void)toAppShowSetting {
    SettingViewController* set=[[SettingViewController alloc]init];
    //隐藏底部导航
    set.hidesBottomBarWhenPushed=YES;
    
    [self.navigationController pushViewController:set animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)refreshView{
    if (_isLoadingMore) {
        _isLoadingMore=NO;
        [_tableView footerEndRefreshing];
    }
    if (_isRefresh) {
        _isRefresh=NO;
         [_tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
        
    }
    //专门刷新列表
    [_tableView reloadData];
}
#pragma mark - Delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _data.applications.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"Identifier";
    ApplicationTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"ApplicationTableViewCell" owner:self options:nil]lastObject];
    }
    //添加渐进指示器，右侧的小箭头
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    AppListModel* model=[_data.applications objectAtIndex:indexPath.row];
    cell.rating.text = model.starCurrent;
    cell.name.text=model.name;
    //[cell.icon sd_setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    [cell.icon setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    cell.share.text=[NSString stringWithFormat:@"分享:%@",model.shares];
    [cell.StarView setStartValue:[model.starCurrent floatValue]];
    cell.favorit.text=[NSString stringWithFormat:@"喜爱:%@",model.favorites];
    cell.download.text=[NSString stringWithFormat:@"下载:%@",model.downloads];
    //cell.backgroundColor=[UIColor blackColor];
    return cell;
}
//重置行高，将cell进行定制
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90.f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //取到数据
    AppListModel* model=[_data.applications objectAtIndex:indexPath.row];
    
    
    //进入详情页面
    AppInfoViewController* avc=[[AppInfoViewController alloc]initWithApplicationID:model.applicationId];
    //当控制器被推送过来，就隐藏底部的bar
    avc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:avc animated:YES];
}
@end
